package com.aurora.oa.home.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.home.bean.ArticleBean;
import com.aurora.oa.home.view.ArticalListActivity;
import com.aurora.oa.home.view.ArticalListActivityView;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
* Created by Administrator on 2017/03/20
*/

public class ArticalListActivityPresenterImpl implements ArticalListActivityPresenter{

    ArticalListActivityView mArticalListActivityView;
    RequestHttpModel mRequestHttpModel;

    public ArticalListActivityPresenterImpl(ArticalListActivityView mArticalListActivityView) {
        this.mArticalListActivityView = mArticalListActivityView;
        mRequestHttpModel = new RequestHttpModelImpl();

    }

    @Override
    public void doPullDownRefresh() {
        String url = AuroraConstants.BASE_SERVER_URL + "/article/page/0";
        mRequestHttpModel.requestHttpDataString(url, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                Gson gson = new Gson();
                Type type = new TypeToken<HttpResult<List<ArticleBean>>>(){}.getType();
                HttpResult<List<ArticleBean>> result =gson.fromJson(body,type);
                mArticalListActivityView.doPullDownRefresh(result.obj);
            }

            @Override
            public void onRequestDataFail(String message) {

            }
        });


    }

    /**
     *
     * @param whitchPage 加载第几页
     */
    @Override
    public void doPullUpLoading(int whitchPage) {
        String url = AuroraConstants.BASE_SERVER_URL + "/article/page/"+whitchPage;
        mRequestHttpModel.requestHttpDataString(url, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                Gson gson = new Gson();
                Type type = new TypeToken<HttpResult<List<ArticleBean>>>(){}.getType();
                HttpResult<List<ArticleBean>> result =gson.fromJson(body,type);
                mArticalListActivityView.doPullUpLoad(result.obj);
            }

            @Override
            public void onRequestDataFail(String message) {

            }
        });
    }
}